package com.laobiao.paChong.wanyijunshi.sina.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.laobiao.paChong.wanyijunshi.commModel.BaseNewsModel;
import com.laobiao.paChong.wanyijunshi.exception.PaChongException;
import com.laobiao.paChong.wanyijunshi.util.HttpClientUtil;
import com.laobiao.paChong.wanyijunshi.util.WebFormatter;

/**
 * 新浪新闻正文model
 * @author laobiao
 *
 */
public class SinaContentModel extends BaseNewsModel{
	public SinaContentModel(Element element) throws ParseException{
		build(element);
		getInfor();
	}
	/**
	 * 
	 * @param element 单条数据的html文档
	 * @param classify 分类
	 * @return
	 * @throws ParseException 
	 */
	public void build(Element element) throws ParseException{
		
		 Elements titleA = element.getElementsByTag("a");
		 Elements dateSpan = element.select("span.time");
		 if(!titleA.isEmpty() && !dateSpan.isEmpty()){
			
			 String href=titleA.attr("href");
			 String title=titleA.text();
			
			 setTitle(title);
			setInforUri(href);
			 setSourceMedia("新浪新闻");
			 //
		 }else{
			 throw new PaChongException("【新浪军事】没有找到标题类数据！");
		 }
	}
	
	private  void getInfor(){
//		HttpClientUtil
		Map<String, String> headers=new HashMap<>();
		headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36");
		headers.put("Accept-Language", "zh-CN,zh;q=0.8");
		headers.put("Accept-Encoding", "gzip, deflate");
		String zwUri = super.getInforUri();
		String doc=HttpClientUtil.sendGetRequest(zwUri, null, headers,"UTF-8");
		Document contentDoc = Jsoup.parse(doc);
		//正文内容
		String content=WebFormatter.html2text(contentDoc.select("#article_content>.article-content-left>#article").html());
		super.setContent(content);
		
		//时间
		 String dateStr = contentDoc.select("#top_bar .date").text();
		super.setNewsDate(dateStr);
		
		//来源
		String media= contentDoc.select("#top_bar .date-source>.source").text();
		super.setEditMedia(media);
		
		//关键词
		Elements keywords = contentDoc.select("#article-bottom>.keywords>a");
		List<String> ztc=new ArrayList<>();
		if(keywords!=null && !keywords.isEmpty()){
			for (Element element : keywords) {
				String keyword = element.text();
				ztc.add(keyword);
			}
		}
		super.setZtc(ztc);
		
		//分类 .path .channel-path>[data-sudaclick=\"cnav_breadcrumbs_p\"]
		Elements classifys = contentDoc.select(".path .channel-path>[data-sudaclick=\"cnav_breadcrumbs_p\"]");
		List<String> classIfyList=new ArrayList<>();
		classIfyList.add("新浪军事");
		for (Element element : classifys) {
			classIfyList.add(element.text());
		}
		super.setClassify(classIfyList);
		
	}
	
	


	
	
	
}
